import * as path from 'path';
import { EXTENSION_ROOT_DIR } from '../../../constants';

export const _SCRIPTS_DIR = path.join(EXTENSION_ROOT_DIR, 'eqFiles');

export const OUTPUT_MARKER_SCRIPT = path.join(_SCRIPTS_DIR, 'get_output_via_markers.py');

const SCRIPTS_DIR = _SCRIPTS_DIR;

type ReleaseLevel = 'alpha' | 'beta' | 'candidate' | 'final';
type EQVersionInfo = [number, number, number, ReleaseLevel, number];

export type InterpreterInfoJson = {
    versionInfo: EQVersionInfo;
    sysPrefix: string;
    sysVersion: string;
    is64Bit: boolean;
};

export function interpreterInfo(): [string[], (out: string) => InterpreterInfoJson | undefined] {
    const script = path.join(SCRIPTS_DIR, 'interpreterInfo.py');
    const args = [script];

    function parse(out: string): InterpreterInfoJson | undefined {
        let json: InterpreterInfoJson | undefined;
        try {
            json = JSON.parse(out);
        } catch (ex) {
            throw Error(`eq ${args} returned bad JSON (${out}) (${ex})`);
        }
        return json;
    }

    return [args, parse];
}
